package com.example.demo.controller;


import com.example.demo.Lending;
import com.example.demo.dao.BookDAO;
import com.example.demo.pojo.Book;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * element -ui 渲染前端页面
 *
 * 展示 图书列表  被借的书籍 不显示接触 按钮
 *
 *
 * 后端 借书有功能
 *
 */


@RestController
@CrossOrigin(origins = "*")
@Api(value = "BookController",tags = "书籍接口")
public class BookController {
    private final BookDAO bookDAO;
    // 构造器注入
    public BookController(BookDAO dao){
        this.bookDAO = dao;
    }

    @ApiOperation(value ="Test Service")
    @RequestMapping(value = "/t1", method = RequestMethod.GET )
    public ResponseEntity test(){
        System.out.println(ResponseEntity.ok());

        return ResponseEntity.ok().body(new Lending("0003", new Date(), "曹叡","Transaction Process"));
    }

    // 测试用 api
    @ApiOperation(value ="Test Service is normal")
    @RequestMapping(value = "/say", method = RequestMethod.GET )
    public List<Lending> sayHello(){
        List<Lending> ret = new ArrayList<Lending>();
        ret.add(new Lending("0001", new Date(), "曹叡","平凡的世界"));
        ret.add(new Lending("0002", new Date(), "曹叡","人生"));
        ret.add(new Lending("0003", new Date(), "曹叡","Transaction Process"));
        ResponseEntity.ok();

        return ret;
    }

    @ApiOperation(value ="通过书名查询分页书籍数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="bookName",value = "书名",required = false),
            @ApiImplicitParam(name = "pageNum",value = "页码",required = false),
            @ApiImplicitParam(name = "pageSize",value = "每页条数",required = false)
    })
    @RequestMapping(path="/books", method = RequestMethod.POST )
    public Map<String, Object> findBooks(@RequestBody Map<String, String> paras,
                         @RequestParam(required = false, defaultValue = "1") Integer pageNum,// 默认第一页 五条数据
                         @RequestParam(required = false, defaultValue = "5") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        Map<String, Object> data = new HashMap();
        List<Book> books = bookDAO.findBooks(paras.get("bookName"));
        PageInfo pageInfo = new PageInfo(books);
        data.put("books", pageInfo.getList());
        data.put("total", pageInfo.getTotal());
        return data;
    }

    @ApiOperation(value ="更新书籍")
    @RequestMapping(path="/update", method = RequestMethod.PUT)
    public void updateBook(@RequestBody Book book){
        bookDAO.updateBook(book);
    }
    @ApiOperation(value ="根据id删除书籍")
    @RequestMapping(path="/delete", method = RequestMethod.DELETE)
    public void deleteBook(@RequestBody Map<String, String> paras){
        bookDAO.deleteBook(paras.get("id"));
    }

}

